//
//  AppDelegate.m
//  NewiOSProject
//
//  Created by 范智渊 on 2018/10/8.
//  Copyright © 2018 范智渊. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <EventKit/EventKit.h>

#import "AppDelegate.h"
#import "SuperTabBarViewController.h"
#import "JMLoginVC.h"
#import "Adjust.h"
#import <CoreLocation/CoreLocation.h>

//#import "LNDetector.h"
#import "JMTenLocationManager.h"
#import "CCLocation.h"
#import "ADJUtil.h"
#import <AdSupport/AdSupport.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>


@interface AppDelegate ()<UIScrollViewDelegate,RDVTabBarControllerDelegate,CLLocationManagerDelegate>{
 
}

@property (nonatomic,strong)SuperTabBarViewController *tabBarViewController;
@property(nonatomic,strong)CLGeocoder * geocoder;
@property(nonatomic,strong)CLLocationManager * locationManager;


@end

@implementation AppDelegate
- (UIWindow *)window
{
    if(!_window) {
        _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _window.backgroundColor = [UIColor whiteColor];
        [_window makeKeyAndVisible];
    }
    return _window;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    JMLoginUserModel *user =  JMProjectManager.sharedJMProjectManager.loginUser;

    delayOperation(100, ^{
        user.lastStartTimeStemp  =[NSString stringWithFormat:@"%ld",[[NSDate new] timeIntervalSince1970] * 1000];
        [user save];
    });
    
    delayOperation(0, ^{
        if(user.token.length){
            self.tabBarViewController =[[SuperTabBarViewController alloc] init];
            self.window.rootViewController = self.tabBarViewController;
        }else{
            UINavigationController *firstNVC = [[UINavigationController alloc]
                                                initWithRootViewController:[NSClassFromString(@"JMLoginVC") new]];
            self.window.rootViewController = firstNVC;
        }
    });
    //监听手机是否开机低电量模式
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangePowerMode:) name:NSProcessInfoPowerStateDidChangeNotification object:nil];

    NSString *environment = ADJEnvironmentProduction;
    ADJConfig*adjustConfig = [ADJConfig configWithAppToken:adjust_token
                                      environment:environment];
    [Adjust appDidLaunch:adjustConfig];
    [adjustConfig setLogLevel:ADJLogLevelVerbose];
    
    if (user.notFirst==NO) {
        ADJEvent *event = [ADJEvent eventWithEventToken:@"za0zyb"];
        [event addCallbackParameter:@"eventKey" value:@"First_Open_Event"];
        [event addCallbackParameter:@"phone" value:user.phone?user.phone:@""];
        [event addCallbackParameter:@"userId" value:user.userId?user.userId:@""];
        [Adjust trackEvent:event];
        user.notFirst = YES;
        [user save];
    }
    
    [self setIDFA];

    return YES;
}

-(void)setIDFA{
    NSString *idfa = [Adjust idfa];
    NSString *adId = [Adjust adid];
    NSString *idfv = [ADJUtil idfv];
    JMProjectManager.sharedJMProjectManager.appConfig.idfv = idfv;
    NSLog(@"idfa=%@\nadid=%@\nidfv=%@",idfa,adId,idfv);
    if (adId != nil && ![adId isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
        // 使用 adId 进行你的操作
        JMProjectManager.sharedJMProjectManager.appConfig.adid = adId;
        JMProjectManager.sharedJMProjectManager.appConfig.idfa = idfa;
        NSLog(@"idfa=%@\nadid=%@\nidfv=%@",JMProjectManager.sharedJMProjectManager.appConfig.idfa,JMProjectManager.sharedJMProjectManager.appConfig.adid,JMProjectManager.sharedJMProjectManager.appConfig.idfv);
        
    } else {
        // adId 尚未准备好，稍后再尝试
       idfa = @"";
       adId = @"";
       idfv =@"";
        NSLog(@"Adjust尚未准备好，稍后再尝试");
    }
}
-(void)applicationDidBecomeActive:(UIApplication *)application{
    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            NSLog(@"status = %lu",(unsigned long)status);
            if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                [self setIDFA];
            }
        }];
    }
}
#pragma clang diagnostic pop
@end
